package messages;

import java.io.Serializable;

/**
 * Root class of messages to send in the system.
 * @author Erik
 *
 */
public class Message implements Serializable{
	/**
	 * Generated UID
	 */
	private static final long serialVersionUID = 7918384711634234085L;
	
	public String ownerID;
	/**
	 * Last hop is used not to forward messages from where they came.
	 */
	public String lastHop;
	public int TTL;

	public static final int maxTTL = 4;
	
	public Message(String ownerID) {
		this.ownerID = ownerID;
		this.TTL = maxTTL; 
	}
	
	/**
	 * MUST BE CALLED BEFORE SENDING!
	 * @param MyName - this nodes name
	 * @return true if message is ready to send
	 */
	public boolean forward(String MyName) {
		if(decrementTTL()) {
			this.lastHop = MyName;
			return true;
		} 
		return false;
	}
	
	/**
	 * Make sure this method is called before re sending!
	 * @return true if we should re send, false otherwise.
	 */
	private boolean decrementTTL() {
		TTL =-1;
		return TTL>0;
	}
}
